﻿using System;
using System.IO;
using log4net;
using log4net.Config;

namespace Golos.Helper
{
        public class LogClass
        {
            static LogClass()
            {
                XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, logFileName)));
            }

            public static void WriteDebugEntry(string message)
            {
                if (logger.IsDebugEnabled)
                    logger.Debug(string.Format("{0}\r\n", message));
            }

            public static void WriteDebugEntry(string message, params object[] parameters)
            {
                if (logger.IsDebugEnabled)
                    logger.Debug(string.Format(message + "\r\n", parameters));
            }

            public static void WriteErrorEntry(string message)
            {
                if (logger.IsErrorEnabled)
                    logger.Error(string.Format("{0}\r\n", message));
            }

            public static void WriteErrorEntry(string message, params object[] parameters)
            {
                if (logger.IsErrorEnabled)
                    logger.Error(string.Format(message + "\r\n", parameters));
            }

            public static void WriteWarningEntry(string message)
            {
                if (logger.IsWarnEnabled)
                    logger.Warn(string.Format("{0}\r\n", message));
            }

            public static void WriteWarningEntry(string message, params object[] parameters)
            {
                if (logger.IsWarnEnabled)
                    logger.Warn(string.Format(message + "\r\n", parameters));
            }

            public static void WriteInfoEntry(string message)
            {
                if (logger.IsInfoEnabled)
                    logger.Info(string.Format("{0}\r\n", message));
            }

            public static void WriteInfoEntry(string message, params object[] parameters)
            {
                if (logger.IsInfoEnabled)
                    logger.Info(string.Format(message + "\r\n", parameters));
            }

            public static void WriteAccessLog(string method, string remoteEndPoint, string url, string responseCode, string responseSize, string responseTime)
            {
                try
                {
                    if (logger.IsInfoEnabled && method == "GET")
                        logger.Info(string.Format("ACCESSLOG\t{0}\t{1}\t{2}\t{3}\t{4}\t{5}\r\n", method, remoteEndPoint, url, responseCode, responseSize, responseTime));
                }
                catch { }

            }

            private static readonly log4net.ILog logger = LogManager.GetLogger(typeof(LogClass));
            private const string logFileName = "log.config.xml";


            public void Info(string format, params object[] parameters)
            {
                WriteInfoEntry(format, parameters);
            }

            public void Warn(string format, params object[] parameters)
            {
                WriteWarningEntry(format, parameters);
            }

            public void Error(string format, params object[] parameters)
            {
                WriteErrorEntry(format, parameters);
            }

            public void Debug(string format, params object[] parameters)
            {
                WriteDebugEntry(format, parameters);
            }
        }

}
